using LargeFilesSample.Models;
using Microsoft.EntityFrameworkCore;

namespace LargeFilesSample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            var configBuilder = new ConfigurationBuilder();
            configBuilder.AddJsonFile("appsettings.json");
            IConfiguration configuration = configBuilder.Build();

            #region MySQL Server IP address, port, username
            var connStr = configuration.GetConnectionString("DocumentStorageCenter");
            builder.Services.AddEntityFrameworkMySql().AddDbContext<DocumentStorageCenterDbContext>(options => options.UseMySql(connStr, ServerVersion.Parse("8.0.29-mysql")));
            #endregion

            #region CORS(Cross-Origin Resource Sharing) Settings
            string corsPolicyAllowAny = "any";
            string corsPolicyAllowOnlyMyIpAddr = "only_my_ip_addr";
            builder.Services.AddCors(options =>
            {
                options.AddPolicy(corsPolicyAllowAny,
                    builder => builder.AllowAnyOrigin()
                       .WithMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
                );
                options.AddPolicy(corsPolicyAllowOnlyMyIpAddr,
                    builder => builder.AllowAnyOrigin()
                       .WithMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
                );
            });
            #endregion

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            app.UseCors(corsPolicyAllowAny);
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}